﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Text;
using Chenxin.Lib.Utility;

public partial class PersonalCenter:PageBase
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindPersonalInfo();
            OpenPhulicInfo();
            GetCurrentDateReport();
            GetPublishInfo();
         
        }
       //控制的部门
       Chenxin.Lib.House.Entity.House_BusinessSetting hbs = Chenxin.Lib.House.DAL.House_BusinessSetting.GetModel(CurrentUserInfo.CompanyId);
       if (hbs != null && hbs.UseBlockRole) 
       {
           houseLink.NavigateUrl = "HouseMgrWithRegion.aspx";
       } else{
           //houseLink.NavigateUrl = "HouseMgr.aspx";
           houseLink.NavigateUrl = "HouseMgrNew1.aspx";
       }
    }

    private void BindPersonalInfo()
    {
        Chenxin.Lib.Passport.Entity.Hr_UserInfo model = Chenxin.Lib.Passport.DAL.Hr_UserInfo.GetEntityById(CurrentUserInfo.UserId);

        //Lit_Company.Text = CurrentUserInfo.CompanyName;
        Lit_Depart.Text = CurrentUserInfo.DepartName;
        Lit_UserName.Text = CurrentUserInfo.UserName;
        Lit_UserCode.Text = CurrentUserInfo.UserCode;
        Lit_Position.Text = model.Position;
        //Lit_EmployeeLevel.Text = model.EmpLevel;
        //Lit_IDNo.Text = model.IDNo;
        Lit_Mobile.Text = model.Mobile;
        //Lit_OfficeTel.Text = model.OfficeTel;
        //Lit_Education.Text = model.Education;
        //Lit_Address.Text = model.Address;
        DataSet ds = Chenxin.Lib.Hr.DAL.Hr_UserLoginLogs.GetLoginInfo(model.UserCode);
        Lit_LoginCount.Text = ds.Tables[0].Rows[0].ItemArray[0].ToString();
        if (ds.Tables[1].Rows.Count >1)
        {
            Lit_LastLoginTime.Text = ds.Tables[1].Rows[1].ItemArray[0].ToString();
        }
        else
        {
            Lit_LastLoginTime.Text = DateTime.Now.ToString();
        }

        StringBuilder sb = new StringBuilder();
        sb.Append("<div>");
        if (!string.IsNullOrEmpty(Constants.DIRECTORY + model.PicUrl))
        {
            sb.AppendFormat("<img src='{0}' width='83' height='100' />", Constants.DIRECTORY + model.PicUrl);
        }
        else
        {
            sb.Append("<img src='/images/pic_img.gif' width='83' height='100' />");        
        }
        sb.Append("</div>");
        Lit_HeadPic.Text = sb.ToString();
    }

    private void OpenPhulicInfo()
    {
        string strWhere = string.Format("InfoType = 50 and isPopUp = 'true' and a.IsDel = 'false' And CompanyId = '{0}' And datediff(dd,getdate(),PublishTime) <= 0 order by Addtime desc", CurrentUserInfo.CompanyId);
        DataSet ds = Chenxin.Lib.Hr.DAL.Pblish_Info.GetList("1", strWhere);

        if (ds != null && ds.Tables[0].Rows.Count > 0)
        {
            StringBuilder strScript = new StringBuilder();

            strScript.Append("<script>");
            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                strScript.Append("window.open('PublishInfo/PublishDetailInfo.aspx?RecordId=" + dr["RecordId"] + "');");
            }
            if (strScript.Length > 8)
            {
                strScript.Append("</script>");
                Page.ClientScript.RegisterStartupScript(this.GetType(), " ", strScript.ToString());
            }
        }
    }
    protected void Btn_StartWork_Click(object sender, EventArgs e)
    {

    }
    protected void Btn_EndWork_Click(object sender, EventArgs e)
    {

    }

    /// <summary>
    /// 获取当前工作报告
    /// </summary>
    private void GetCurrentDateReport()
    {
        DataSet ds = Chenxin.Lib.Hr.DAL.WorkReport.GetCurrentReport(CurrentUserInfo.UserId, 5);

        StringBuilder sb = new StringBuilder();

        sb.Append("<table style=\"width:100%;\" cellspacing=\"1\" class=\"tabsorternew\" id=\"report\">");
        sb.Append("<thead>");
        sb.Append("<tr><th style=\"width:20px;\"></th><th>主题</th>");
        sb.Append("<th style=\"width:120px\">发送时间</th>");
        sb.Append("</tr></thead>");
        sb.Append("<tbody>");

        if (ds.Tables[0].Rows.Count > 0)
        {
            int index = 0;
            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                index++;
                sb.AppendFormat("<tr><td>{3}</td><td><a href=\"/WorkReport/WorkReportDetailInfo.aspx?RecordId={0}\" title=\"{1}\">{1}</a></td><td>{2}</td></tr>", dr["RecordId"], dr["Subject"], dr["SendTime"],index);
            }
        }
        else
        {
            sb.AppendFormat("<tr><td colspan=\"3\" style=\"text-align:center;\">当前没有工作报告！</td></tr>");
        }
        sb.Append("</tbody>");
        sb.Append("</table>");

        Lit_CurrentDateReport.Text = sb.ToString();
    }

    private void GetPublishInfo()
    {
        StringBuilder sb = new StringBuilder();
        sb.AppendFormat("a.RecordId in (select top 5 b.RecordId from Pblish_Info b where b.InfoType = 0 and datediff(dd,getdate(),PublishTime) <= 0 and IsDel = 0 and companyid='{0}' order by PublishTime desc)", CurrentUserInfo.CompanyId);
        sb.AppendFormat("or a.RecordId in (select top 5 c.RecordId from Pblish_Info c where c.InfoType = 10 and datediff(dd,getdate(),PublishTime) <= 0 and IsDel = 0 and companyid='{0}' order by PublishTime desc)", CurrentUserInfo.CompanyId);
        sb.AppendFormat("or a.RecordId in (select top 5 d.RecordId from Pblish_Info d where d.InfoType = 20 and datediff(dd,getdate(),PublishTime) <= 0 and IsDel = 0 and companyid='{0}' order by PublishTime desc)", CurrentUserInfo.CompanyId);
        sb.AppendFormat("or a.RecordId in (select top 5 e.RecordId from Pblish_Info e where e.InfoType = 30 and datediff(dd,getdate(),PublishTime) <= 0 and IsDel = 0 and companyid='{0}' order by PublishTime desc)", CurrentUserInfo.CompanyId);
        sb.AppendFormat("or a.RecordId in (select top 5 f.RecordId from Pblish_Info f where f.InfoType = 40 and datediff(dd,getdate(),PublishTime) <= 0 and IsDel = 0 and companyid='{0}' order by PublishTime desc)", CurrentUserInfo.CompanyId);
        sb.AppendFormat("or a.RecordId in (select top 5 g.RecordId from Pblish_Info g where g.InfoType = 50 and datediff(dd,getdate(),PublishTime) <= 0 and IsDel = 0 and companyid='{0}' order by PublishTime desc)", CurrentUserInfo.CompanyId);
        sb.Append("order by a.PublishTime desc");

        DataSet ds = Chenxin.Lib.Hr.DAL.Pblish_Info.GetList("30", sb.ToString());

        Lit_PublishInfo.Text += CreatePublishInfo(ds.Tables[0], "0", "公告信息");
        Lit_PublishInfo.Text += CreatePublishInfo(ds.Tables[0], "10", "通知信息");
        Lit_PublishInfo.Text += CreatePublishInfo(ds.Tables[0], "20", "一手房信息");
        Lit_PublishInfo.Text += CreatePublishInfo(ds.Tables[0], "30", "二手房信息");
        Lit_PublishInfo.Text += CreatePublishInfo(ds.Tables[0], "40", "其他信息");
        Lit_PublishInfo.Text += CreatePublishInfo(ds.Tables[0], "50", "特别信息");
    }

    private string CreatePublishInfo(DataTable dt,string infoType, string title)
    {
        StringBuilder str = new StringBuilder();
        str.AppendFormat("<div class=\"publishinfoitems\" id=\"{0}\"><h2 class=\"search_table_title\">{1}</h2><ul>", infoType, title);
        DataRow[] drList = dt.Select(string.Format("InfoType='{0}'", infoType));
        if (drList.Length > 0)
        {
            int count = 0;
            foreach (DataRow dr in drList)
            {
                count++;
                str.AppendFormat("<li><a href=\"/PublishInfo/PublishDetailInfo.aspx?RecordId={0}\" title=\"\" >{1}</a><span>{2}</span></li>", dr["RecordId"], dr["Subject"], Convert.ToDateTime(dr["PublishTime"]).ToString("yyyy-MM-dd"));
            }
            for (int i = 0; i < 5 - count; i++)
            {
                str.Append("<li>&nbsp;</li>");

            }

        }
        else
        {
            str.AppendFormat("<li style=\"text-align:center;\">没有{0}记录！</li>", title);
        }
        str.AppendFormat("</ul><span><a href=\"/PublishInfo/PublicInfo.aspx?Type={0}\" title=\"更多...\">更多...</a></span></div>",infoType);
        return str.ToString();
    }
}
